package com.quectel.core.module.forum.service.impl.mongo;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.MongoCollectionConstants;
import com.quectel.core.module.MongoAuthComponent;
import com.quectel.core.module.forum.dto.mongo.ForumLikeDto;
import com.quectel.core.module.forum.entity.mongo.ForumLikeModel;
import com.quectel.core.module.forum.service.mongo.ForumLikeService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.common.Request;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mongo.MongodbUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/5/8 15:52
 * @description 帖子
 */
@DubboService
public class ForumLikeServiceImpl implements ForumLikeService {

    @Autowired
    private MongoAuthComponent mongoAuthComponent;

    private Criteria sqlWhere(Map<String, Object> params) {
        Request request = Request.configParams(params);

        Criteria authByAllQuery = mongoAuthComponent.buildTenantConstraintsQuery(params
                , LambdaUtil.getFieldName(ForumLikeModel::getPersonId)
        );

        return new Criteria().andOperator(authByAllQuery);
    }

    @Override
    public ForumLikeDto selectByPostAndOperator(Long postId, Long operateId) {
        Query query = new Query();
        query.addCriteria(Criteria.where(LambdaUtil.getFieldName(ForumLikeDto::getForumPostId)).is(postId));
        query.addCriteria(Criteria.where(LambdaUtil.getFieldName(ForumLikeDto::getPersonId)).is(operateId));
        ForumLikeModel one = MongodbUtils.findOneData(ForumLikeModel.class,
                query
                , MongoCollectionConstants.FORUM_LIKE);

        return CopyUtils.copyObj(one, ForumLikeDto.class);
    }

    @Override
    public void deleteByPostAndOperator(Long postId, Long operateId) {
        Query query = new Query();
        query.addCriteria(Criteria.where(LambdaUtil.getFieldName(ForumLikeDto::getForumPostId)).is(postId));
        query.addCriteria(Criteria.where(LambdaUtil.getFieldName(ForumLikeDto::getPersonId)).is(operateId));
        MongodbUtils.remove(query, MongoCollectionConstants.FORUM_LIKE);
    }

    @Override
    public ForumLikeDto selectById(Long id) {
        ForumLikeModel one = MongodbUtils.findOne(id, MongoCollectionConstants.FORUM_LIKE, ForumLikeModel.class);
        return CopyUtils.copyObj(one, ForumLikeDto.class);
    }

    @Override
    public List<ForumLikeDto> queryList(Map<String, Object> params) {
        Request request = Request.configParams(params);
        return
                MongodbUtils.page(
                        new Query(sqlWhere(params)),
                        request.getPageNo() - 1,
                        request.getPageSize(),
                        Sort.by(Sort.Direction.DESC, SystemConstants.MONGO_ENTITY_ID_FLAG),
                        ForumLikeModel.class,
                        MongoCollectionConstants.FORUM_LIKE
                ).stream().map(model -> {
                    ForumLikeDto dto = CopyUtils.copyObj(model, ForumLikeDto.class);

                    return dto;
                }).collect(Collectors.toList());
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return (int) MongodbUtils.count(new Query(sqlWhere(params)), MongoCollectionConstants.FORUM_LIKE);
    }

    @Override
    public Long save(ForumLikeDto dto) {
        ForumLikeModel entity = CopyUtils.copyObj(dto, ForumLikeModel.class);
        entity.setId(Snowflake.nextId());
        MongodbUtils.insert(entity, MongoCollectionConstants.FORUM_LIKE);
        return entity.getId();
    }


    @Override
    public void deleteById(Long id) {
        MongodbUtils.delete(id, MongoCollectionConstants.FORUM_LIKE);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            MongodbUtils.delete(id, MongoCollectionConstants.FORUM_LIKE);
        }
    }

}
